package com.farmer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.farmer.pojo.Agritainment;
import com.farmer.service.AgritainmentService;
import com.farmer.mapper.AgritainmentMapper;
import com.farmer.utils.Result;
import com.farmer.utils.ResultCodeEnum;
import com.farmer.utils.Upload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.DateFormat;
import java.util.Date;
import java.util.List;

/**
* @author 86183
* @description 针对表【agritainment(农家乐信息表)】的数据库操作Service实现
* @createDate 2024-04-27 18:11:54
*/
@Service
public class AgritainmentServiceImpl extends ServiceImpl<AgritainmentMapper, Agritainment>
    implements AgritainmentService {
    @Autowired
    private AgritainmentMapper agritainmentMapper;

    //    @Override
//    public Result add(Agritainment agritainment,MultipartFile file) {
//        try{
//        String url =Upload.uploadImage(file);
//        agritainment.setImg(url);
//        agritainment.setTime(DateFormat.getDateInstance().format(new Date()));
//        agritainmentMapper.insert(agritainment);
//        return Result.ok(null);
//    }
//        catch (IOException e) {
//            e.printStackTrace();
//            return Result.build(null, ResultCodeEnum.FAULT);
//        }}
    @Override
    public Result add(Agritainment agritainment) {
        return Result.ok(null);
    }

    @Override
    public Result delete(Integer id) {
        agritainmentMapper.deleteById(id);
        return null;
    }

    @Override
    public Result deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            agritainmentMapper.deleteById(id);
        }
        return Result.ok(null);
    }

    @Override
    public Result update(Agritainment agritainment) {
        agritainmentMapper.updateById(agritainment);
        return Result.ok(null);
    }


    @Override
    public Result getById(Integer id) {
        Agritainment agritainment = agritainmentMapper.selectById(id);
        if (agritainment != null) {
            return Result.build(agritainment, 111, "查询成功");
        } else {
            return Result.build(null, 101, "查询失败");
        }
    }

    @Override
    public Result getAll() {
        List<Agritainment> agritainments = agritainmentMapper.selectList(null);
        if (agritainments != null && !agritainments.isEmpty()) {
            return Result.build(agritainments, 111, "查询成功");
        } else {
            return Result.build(null, 101, "查询失败");
        }

    }

    /*
     * 根据标题模糊查询*/
    @Override
    public Result getByTitleLike(String title) {
        QueryWrapper<Agritainment> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name", title);
        List<Agritainment> agritainments = agritainmentMapper.selectList(queryWrapper);
        if (agritainments != null && !agritainments.isEmpty()) {
            return Result.build(agritainments, 111, "查询成功");
        } else {
            return Result.build(null, 101, "未找到匹配的信息");
        }
    }

    @Override
    public Result show() {
        QueryWrapper<Agritainment> queryWrapper = new QueryWrapper<>();
        queryWrapper.last("limit 6");
        List<Agritainment> agritainments = agritainmentMapper.selectList(queryWrapper);
        return Result.ok(agritainments);
    }

    @Override
    public Result ImgInsert(Integer id, MultipartFile file, String name, String content) {
        try {
            String url = Upload.uploadImage(file);
            System.out.println("图片的url：" + url);
            // 首先检查ID是否已存在于数据库中
            Agritainment agritainment = agritainmentMapper.selectById(id);
            if (agritainment == null) {
                // ID不存在，添加新记录
                Agritainment newAgritainment = new Agritainment();
                newAgritainment.setId(id);
                newAgritainment.setImg(url);
                newAgritainment.setName(name);
                newAgritainment.setContent(content);
                newAgritainment.setTime(DateFormat.getDateInstance().format(new Date()));
                agritainmentMapper.insert(newAgritainment);
                return Result.build(url, ResultCodeEnum.SUCCESS);
            } else {
                // ID存在，更新现有记录的图片URL
                agritainment.setImg(url);
                agritainmentMapper.updateById(agritainment);
                return Result.build(url, ResultCodeEnum.SUCCESS);
            }
        } catch (IOException e) {
            e.printStackTrace();
            return Result.build(null, ResultCodeEnum.FAULT);
        }
//        try {
//            String url = Upload.uploadImage(file);
//            System.out.println("图片的url：" + url);
//          Agritainment agritainment = agritainmentMapper.selectById(id);
//
//            // 更新数据库中商品的图片
//            agritainment.setId(id);
//            agritainment.setImg(url); // 将 OSS 返回的头像 URL 存储到数据库中
//            updateById(agritainment);
//            return Result.build(url, ResultCodeEnum.SUCCESS);
//        } catch (IOException e) {
//            e.printStackTrace();
//            return Result.build(null, ResultCodeEnum.FAULT);
//        }
    }




    /*
     * 分页查询*/
    @Override
    public Result page(Integer pageNum, Integer pageSize){
        // 创建分页构造对象
        Page<Agritainment> page = new Page<>(pageNum, pageSize);
        // 执行分页查询
        IPage<Agritainment> result = agritainmentMapper.selectPage(page, null);
        // 封装到统一响应体Result中
        return Result.ok(result);
    }
}




